// Package service
// Created by GoLand
// @User: lenora
// @Date: 2024/7/3
// @Time: 17:16

package service

import (
	"camp/microbook/interactive/domain"
	"camp/microbook/interactive/repository"
	"context"
)

//go:generate mockgen -source=./interactive.go -package=svcmocks -destination=./mocks/interactive_gen.go
type InteractiveService interface {
	IncrReadCnt(ctx context.Context, biz string, bizId uint64) error
	IncrLikeCnt(ctx context.Context, biz string, id, userId uint64) error
	DecrLikeCnt(ctx context.Context, biz string, id, userId uint64) error
	Collect(ctx context.Context, biz string, id, cid, userId uint64) error
	Get(ctx context.Context, biz string, id, userId uint64) (domain.Interactive, error)
	GetByIds(ctx context.Context, biz string, ids []uint64) (map[uint64]domain.Interactive, error)
}

func NewInteractiveService(repo repository.InteractiveRepository) InteractiveService {
	return &InteractiveServiceImpl{repo: repo}
}

type InteractiveServiceImpl struct {
	repo repository.InteractiveRepository
}

func (svc *InteractiveServiceImpl) GetByIds(ctx context.Context, biz string, ids []uint64) (map[uint64]domain.Interactive, error) {
	res, err := svc.repo.GetByIds(ctx, biz, ids)
	if err != nil {
		return nil, err
	}
	data := make(map[uint64]domain.Interactive)
	for _, item := range res {
		data[item.BizId] = item
	}
	return data, nil
}

func (svc *InteractiveServiceImpl) Get(ctx context.Context, biz string, id, userId uint64) (domain.Interactive, error) {
	return svc.repo.Get(ctx, biz, id, userId)
}

func (svc *InteractiveServiceImpl) Collect(ctx context.Context, biz string, id, cid, userId uint64) error {
	return svc.repo.IncrCollectCnt(ctx, biz, id, cid, userId)
}

func (svc *InteractiveServiceImpl) IncrLikeCnt(ctx context.Context, biz string, id, userId uint64) error {
	return svc.repo.IncrLikeCnt(ctx, biz, id, userId)
}

func (svc *InteractiveServiceImpl) DecrLikeCnt(ctx context.Context, biz string, id, userId uint64) error {
	return svc.repo.DecrLikeCnt(ctx, biz, id, userId)
}

// IncrReadCnt  增加阅读数
// @param biz 通过何种方式增加阅读记数
// @param bizId 文章id
// @return error
func (svc *InteractiveServiceImpl) IncrReadCnt(ctx context.Context, biz string, bizId uint64) error {
	return svc.repo.IncrReadCnt(ctx, biz, bizId)
}
